<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <div class="w-full mt-2 mb-2">
      <FieldForm :queryId="queryId" ref="fieldFormRef" />
    </div>
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, ref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import {
    getQueryGetConditions,
    getQueryModifyConditionFieldCode,
    getQueryUpdateConditions,
  } from '/@/api/reportForms/reportForms';
  import FieldForm from '../searchDesigner/fieldForm/index.vue';
  export default defineComponent({
    name: 'SearchTableCondition',
    components: {
      BasicModal,
      FieldForm,
    },
    emits: ['success', 'register'],
    setup(_, { emit }) {
      const { createMessage } = useMessage();
      const isUpdate = ref(false);
      const fieldFormRef = ref<any>(null);
      const queryId = ref('');
      const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
        setModalProps({ confirmLoading: false, width: '800px' });
        try {
          isUpdate.value = data?.isUpdate;
          queryId.value = data?.id;
          setTimeout(() => {
            fieldFormRef.value.fetch();
          });
          console.log('执行了', fieldFormRef.value);
        } catch (err) {
          console.error(err);
        }
      });

      const getTitle = '条件';

      async function handleSubmit() {
        try {
          setModalProps({ confirmLoading: true });
          const fieldForm = fieldFormRef.value.dataRef;
          let params = {
            id: queryId.value,
            fieldForm,
          };
          console.log('fieldForm', fieldForm);
          await getQueryUpdateConditions(params);
          createMessage.success('操作成功');
          closeModal();
          emit('success');
        } catch (err) {
          console.error(err);
        } finally {
          setModalProps({ confirmLoading: false });
        }
      }

      return {
        registerModal,
        getTitle,
        handleSubmit,
        fieldFormRef,
        queryId,
      };
    },
  });
</script>
